﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using ESHOP.Domain;
using ESHOP.UTIL;
using System.Data.Common;
namespace ESHOP.DAO
{
    public class AccountDAO :BaseDAO
    {
      
        public ACOUNT CheckLogin(String user, String pass)
        {
            return _db.ACOUNTs.Where(p => p.USERNAME.Equals(user) && p.PASS.Equals(pass)&& p.ISACTIVE.Equals(true)).SingleOrDefault();
        }
        public ACOUNT Get(String user)
        {
            return _db.ACOUNTs.Where(p => p.USERNAME.Equals(user)&& p.ISACTIVE.Equals(true)).SingleOrDefault();
        }
        public int Update(ACOUNT objui)
        {
           
                var objdb = Get(objui.USERNAME);
                if (objdb != null)
                {
                    objdb.PASS = objui.PASS;
                    
                    objdb.MACT = objui.MACT;
                    objdb.ISACTIVE = objui.ISACTIVE;
                    _db.SubmitChanges();
                    return 1;
                }
             
            
          
            return 0;
        }
        public int UpdateAcountprofile(ACOUNT objui)
        {

            var objdb = Get(objui.USERNAME);
            if (objdb != null)
            {
                objdb.FULLNAME = objui.FULLNAME;
                objdb.JOBTITLE = objui.JOBTITLE;
                objdb.MAQG = objui.MAQG;
                objdb.CONTACTDDRESS =objui.CONTACTDDRESS;
                objdb.EMAIL = objui.EMAIL;
                objdb.PHONE = objui.PHONE;
                objdb.ISACTIVE = objui.ISACTIVE;
                _db.SubmitChanges();
                return 1;
            }



            return 0;
        }
        public int Changepass(ACOUNT objui, String _newpass)
        {
            try{
                var objdb = Get(objui.USERNAME);
                if (objdb != null)
                {
                    objdb.PASS = _newpass;
                   
                }
                _db.SubmitChanges();
            }
            catch
            {
                return 0;
            }
            return 1;
        }

        public int Delete(ACOUNT objui)
        {
            DbTransaction trans = null;
            try
            {
                _db.Connection.Open();
                trans = _db.Connection.BeginTransaction();
                var objdb = Get(objui.USERNAME);
                if (objdb != null)
                {
                    objdb.ISACTIVE = false;

                }
                _db.SubmitChanges();
                trans.Commit();

            }
            catch
            {
                if (trans != null)
                    trans.Rollback();
                return 0;
            }
            return 1;
        }
        public int Shift_Delete(ACOUNT objui)
        {
            DbTransaction trans = null;
            try
            {
                _db.Connection.Open();
                trans = _db.Connection.BeginTransaction();
                var objdb = Get(objui.USERNAME);
                if (objdb != null)
                {
                    _db.ACOUNTs.DeleteOnSubmit(objdb);

                }
                _db.SubmitChanges();
                trans.Commit();

            }
            catch
            {
                if (trans != null)
                    trans.Rollback();
                return 0;
            }
            return 1;
        }
        public int Insert(ACOUNT objui)
        {
            try
            {
                _db.ACOUNTs.InsertOnSubmit(objui);
                _db.SubmitChanges();
                return 1;
            }
            catch
            {
                return 0;
            }
        }
        public List<ACOUNT> Search(String NameCongTy, String QuocGia, String Email, String Phone)
        {
            var list = _db.ACOUNTs.AsQueryable();
            if (!String.IsNullOrEmpty(NameCongTy))
            {
                list = list.Where(p => (p.JOBTITLE.Contains(NameCongTy) || p.FULLNAME.Contains(NameCongTy))).AsQueryable();
            }
            if (!String.IsNullOrEmpty(QuocGia))
            {
                list = list.Where(p => p.MAQG.Equals(QuocGia)).AsQueryable();
            }
            if (!String.IsNullOrEmpty(Email))
            {
                list = list.Where(p => p.EMAIL.Equals(Email)).AsQueryable();
            }
            if (!String.IsNullOrEmpty(Phone))
            {
                list = list.Where(p => p.PHONE.Equals(Phone)).AsQueryable();
            }
            return list.ToList();
        }

        public int ActiveProduct(ACOUNT obj)
        {
            var sanpham = Get(obj.USERNAME);
            if (sanpham != null)
            {
                sanpham.ISACTIVE = true;
                _db.SubmitChanges();
                return 1;
            }
            return 0;
        }
        public int DeActiveProduct(ACOUNT obj)
        {
            var sanpham = Get(obj.USERNAME);
            if (sanpham != null)
            {
                sanpham.ISACTIVE = false;
                _db.SubmitChanges();
                return 1;
            }
            return 0;
        }
    }
}
